<?php
/**
 * 消息模板
 * Created by PhpStorm.
 * User: yangxiaolin
 * Date: 2017/4/26
 * Time: 15:07
 */

namespace App\Services;


use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Config;

trait BankServices
{
    use BaseServices;
    public $signKey = "6YCNNRWAPBYPLQT6P839XMEMLNJCD6A6KXUI74G3J4PSZG8KM1Z5SYHU3EWY";
    public $merId = '1000000001';
    /**
     * 给银行下发获取文件
     *
     * @param $data  [park_code,start_time,end_time,msgno,msg,mode]
     * @return json
     */
    public function sendBankInfo()
    {
        //定义数组
        $parameter = array(
            'version' => '2.0',                        //版本号
            'charset' => 'UTF-8',                        //字符编码
            'signMethod' => 'MD5',                //签名方法
            'transCode' => 'F001',                    //交易码
            'merId' => $this->merId,                            //商户编号
            'billDate' => str_replace("-",'',date("Y-m-d")),                    //日期
            'billType' => '01',                    //对账单类型
            'fileType' => '02',                    //文件类型
            'transTime' => str_replace(["-"," ",":"],['','',''],date("Y-m-d H:i:s")),                    //交易时间
            'backEndUrl' => config('custom.bank_air_api_url'),                //通知URl
        );
        //去除空键
        foreach ($parameter as $key => $value) {
            if ($value === "" || $value === null) {
                unset($parameter[$key]);
            }
        }
        //排序
        ksort($parameter);
        $sign = $this->calcSign($parameter,$this->signKey);
        $parameter['sign'] = $sign;
        $res = sendHttpRequest(config('custom.bank_api_url'), $parameter);

        return $res;
    }

    /**
     * 给银行下发获取文件
     *
     * @param $data  [park_code,start_time,end_time,msgno,msg,mode]
     * @return json
     */
    public function dateBankInfo($date)
    {
        //定义数组
        $parameter = array(
            'version' => '2.0',                        //版本号
            'charset' => 'UTF-8',                        //字符编码
            'signMethod' => 'MD5',                //签名方法
            'transCode' => 'F001',                    //交易码
            'merId' => $this->merId,                            //商户编号
            'billDate' => $date,                    //日期
            'billType' => '01',                    //对账单类型
            'fileType' => '02',                    //文件类型
            'transTime' => str_replace(["-"," ",":"],['','',''],date("Y-m-d H:i:s")),                    //交易时间
            'backEndUrl' => config('custom.bank_air_api_url'),                //通知URl
        );
        //去除空键
        foreach ($parameter as $key => $value) {
            if ($value === "" || $value === null) {
                unset($parameter[$key]);
            }
        }
        //排序
        ksort($parameter);
        
        \Log::info("send data--".json_encode($parameter));

        $sign = $this->calcSign($parameter,$this->signKey);
        $parameter['sign'] = $sign;
        $res = sendHttpRequest(config('custom.bank_api_url'), $parameter);
        \Log::info($res);

        return $res;
    }

    public function downBankfile($data)
    {
        header('Content-type:text/html;charset=utf-8');
        //定义数组
        $parameter = array(
            'version' => '2.0',                        //版本号
            'charset' => 'UTF-8',                        //字符编码
            'signMethod' => 'MD5',                //签名方法
            'transCode' => 'F002',                    //交易码
            'merId' => $this->merId,                            //商户编号
            'billDate' => $data["billDate"],                    //日期
            'billType' => '01',                    //对账单类型
            'fileType' => '02',                    //文件类型
            'transTime' => str_replace(["-"," ",":"],['','',''],date("Y-m-d H:i:s")),                    //交易时间
            'fileName' => $data["fileName"]                //文件名称
        );
        //去除空键
        foreach ($parameter as $key => $value) {
            if ($value === "" || $value === null) {
                unset($parameter[$key]);
            }
        }
        \Log::info("send data--".json_encode($parameter));
        //排序
        ksort($parameter);
        $sign = $this->calcSign($parameter,$this->signKey);
        $parameter['sign'] = $sign;
        $results = sendHttpRequest(config('custom.bank_api_url'), $parameter);
        \Log::info($results);
        if (strpos($results, "respCode=00") !== false) {
            $str = substr($results, strpos($results, "fileContent=") + 12);
            $fileContent = substr($str, 0, strpos($str, "&"));
            $content = base64_decode($fileContent);
            $filePath = storage_path().'/app/bank/data.zip';
            if(file_exists($filePath)){
                unlink($filePath);
                \Log::info('获取完毕');
            }
            file_put_contents($filePath,$content);
            unZip($filePath);
        } else {
            echo $results;
        }
    }

    function calcSign($parameter,$signKey)
    {
        $join = "";
        foreach ((array)$parameter as $key => $val) {
            $join .= $key . "=" . $val . "&";
        }

        $signSrc = $join . md5($signKey);
        return md5($signSrc);
    }



}
